import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author gao
 * @date 2025/10/16 8:34
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public int maxDepth (TreeNode root) {
        if (root == null) {
            return 0;
        }

        int left = maxDepth(root.left);
        int right = maxDepth(root.right);

        return Math.max(left, right) + 1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = in.nextInt();
            }
            TreeNode root = createTree(nums);
            Solution solution = new Solution();
            System.out.println(solution.maxDepth(root));
        }
    }

    private static TreeNode createTree(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);

        int i = 1;

        while (!queue.isEmpty() && i < nums.length) {
            TreeNode curNode = queue.poll();
            if (i < nums.length && nums[i] != -1) {
                curNode.left = new TreeNode(nums[i]);
                queue.offer(curNode.left);
            }
            i++;

            if (i < nums.length && nums[i] != -1) {
                curNode.right = new TreeNode(nums[i]);
                queue.offer(curNode.right);
            }
            i++;
        }
        return root;
    }
}